﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SMALLERP.ComClass;
using SMALLERP.DataClass;
using System.Data.SqlClient;
using XJLSC.PM;
using System.Collections.Specialized;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.IO.Compression;
using System.Net;
using System.Data.OleDb;
using System.Collections;
using System.Reflection;
using SMALLERP;
using DevExpress.XtraEditors;

namespace XJLSC.SO
{
    public partial class DDPHZB : Form
    {
        private readonly DataBase db = new DataBase();
        private SqlDataReader sdr1;
        public DDPHZB()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string strSql = "";

            strSql = "SELECT V_WLPH.TD004 as 品号,ITEM.ITEM_NAME as 品名,ITEM_SPECIFICATION as 规格,ITEM_PROPERTY 品号属性,ITEM.UDF021 as 备注,CEILING(V_WLPH.DDSL) as 订单数量,CEILING(V_WLPH.KCSL) as 库存数量,CEILING(V_WLPH.YJSC) as 预计生产,CEILING(V_WLPH.YJQG) as 预计请购,CEILING(V_WLPH.YJJH+V_WLPH.CGDY) as 预计进货,CEILING(V_WLPH.CGDY) 采购待验,CEILING(V_WLPH.YJLL) as 预计领料,CEILING(V_WLPH.CGDY+V_WLPH.YJJH+V_WLPH.YJQG+V_WLPH.YJSC-V_WLPH.DDSL-V_WLPH.YJLL+V_WLPH.KCSL) as 平衡状况  FROM V_WLPH  Left JOIN ITEM On V_WLPH.TD004=ITEM.ITEM_CODE LEFT JOIN ITEM_PLANT ON ITEM.ITEM_BUSINESS_ID=ITEM_PLANT.ITEM_ID AND V_WLPH.PLANT_ID=ITEM_PLANT.Owner_Org_ROid WHERE (V_WLPH.TD004 LIKE '" + textBox1.Text + "%' OR '" + textBox1.Text + "'='') AND (V_WLPH.TD004 >= '" + textBox6.Text + "' OR '" + textBox6.Text + "'='') AND (V_WLPH.TD004 <= '" + textBox7.Text + "' OR '" + textBox7.Text + "'='')";

            //textBox6.Text = strSql;
            //MessageBox.Show(strSql,"提示");
            try
            {
                DataTable dt;
                this.gridControl1.DataSource = "";
                gridControl1.Refresh();
                dt = db.GetDataSet1(strSql, "PURTD").Tables["PURTD"];
                gridControl1.DataSource = dt;
                gridView1.OptionsView.EnableAppearanceEvenRow = true;
                gridView1.OptionsView.EnableAppearanceOddRow = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "软件提示");
                //throw ex;
            }



            //double sum1 = 0;
            //for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            //{
            //    sum1 += double.Parse(this.dataGridView1.Rows[i].Cells["订单数量"].Value.ToString());
            //}
            //textBox9.Text = sum1.ToString();

            //double sum2 = 0;
            //for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            //{
            //    sum2 += double.Parse(this.dataGridView1.Rows[i].Cells["库存数量"].Value.ToString());
            //}
            //textBox10.Text = sum2.ToString();


            //double sum3 = 0;
            //for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            //{
            //    sum3 += double.Parse(this.dataGridView1.Rows[i].Cells["预计生产"].Value.ToString());
            //}
            //textBox11.Text = sum3.ToString();

            //double sum4 = 0;
            //for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            //{
            //    sum4 += double.Parse(this.dataGridView1.Rows[i].Cells["预计请购"].Value.ToString());
            //}
            //textBox12.Text = sum4.ToString();

            //double sum5 = 0;
            //for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            //{
            //    sum5 += double.Parse(this.dataGridView1.Rows[i].Cells["预计进货"].Value.ToString());
            //}
            //textBox13.Text = sum5.ToString();

            //double sum6 = 0;
            //for (int i = 0; i <= this.dataGridView1.Rows.Count - 1; i++)
            //{
            //    sum6 += double.Parse(this.dataGridView1.Rows[i].Cells["预计领料"].Value.ToString());
            //}
            //textBox14.Text = sum5.ToString();

        }

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string x1 = "";
            string x2 = "";
            double x3 = 0.00;
            string x4 = "";
            string x5 = "";
            //x1 = dataGridView1["品号", dataGridView1.CurrentCell.RowIndex].Value.ToString();
   

            //if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["预测数量"].Index)
            //{
            //    x4 = "查看销售预测";

            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["订单数量"].Index)
            //{
            //    x4 = "查看订单";

            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["库存数量"].Index)
            //{
            //    x4 = "查看库存";

            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["预计生产"].Index)
            //{
            //    x4 = "查看工单";

            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["预计领料"].Index)
            //{
            //    x4 = "查看工单单身";

            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["预计进货"].Index)
            //{
            //    x4 = "查看采购订单";

            //}
            //else if (e.RowIndex > -1 && e.ColumnIndex == this.dataGridView1.Columns["预计请购"].Index)
            //{
            //    x4 = "查看请购单";

            //}
            //else
            //{
            //    x4 = "其他";
            //    MessageBox.Show("本列无明细数据", "软件提示");
            //}

            //if (x4 != "其他")
            //{
            //    MXCX xx = new MXCX(x1, x2, x3, x4,x5);
            //    CHAXUN f = new CHAXUN();
            //    f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
            //    f.Show(this);
            //}
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            DialogResult dialogResult = fileDialog.ShowDialog(this);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                gridControl1.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
                        //decimal t = 0;

                        //for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
                        //{
                        //    if(dataGridView1.SelectedCells[i].Value.GetType() != typeof(String))
                        //    {
                        //    t = t + Convert.ToDecimal(dataGridView1.SelectedCells[i].Value);
                        //    }
                        //}
                        //textBox15.Text = t.ToString();

        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            //decimal t = 0;

            //for (int i = 0; i < dataGridView1.SelectedCells.Count; i++)
            //{
            //    if (dataGridView1.SelectedCells[i].Value.GetType() != typeof(String))
            //    {
            //        t = t + Convert.ToDecimal(dataGridView1.SelectedCells[i].Value);
            //    }
            //}
            //textBox15.Text = t.ToString();

        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void gridControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {

        }

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            string x1 = "";
            string x2 = "";
            double x3 = 0.00;
            string x4 = "";
            string x5 = "";
            x1 = this.gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "品号").ToString();

            if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预测数量")
            {
                x4 = "查看销售预测";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "订单数量")
            {

                x4 = "查看订单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "库存数量")
            {
                x4 = "查看库存";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计生产")
            {
                x4 = "查看工单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计领料")
            {
                x4 = "查看工单单身";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计进货")
            {
                x4 = "查看采购订单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计请购")
            {
                x4 = "查看请购单";

            }
            else
            {
                x4 = "其他";
                MessageBox.Show("本列无明细数据", "软件提示");
            }

            if (x4 != "其他")
            {
                MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                CHAXUN f = new CHAXUN();
                f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                f.Show(this);
            }

        }

        private void gridView1_MouseDown(object sender, MouseEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hitInfo = gridView1.CalcHitInfo(new Point(e.X,e.Y));
            //行双击事件
            if(e.Button==MouseButtons.Left && e.Clicks==2)
            {
                if(hitInfo.InRow)
                {
                    

                }
            }
        }

        private void 查看明细ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string x1 = "";
            string x2 = "";
            double x3 = 0.00;
            string x4 = "";
            string x5 = "";
            x1 = this.gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "品号").ToString();

            if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预测数量")
            {
                x4 = "查看销售预测";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "订单数量")
            {

                x4 = "查看订单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "库存数量")
            {
                x4 = "查看库存";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计生产")
            {
                x4 = "查看工单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计领料")
            {
                x4 = "查看工单单身";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计进货")
            {
                x4 = "查看采购订单";

            }
            else if (gridView1.FocusedRowHandle > -1 && gridView1.FocusedColumn.FieldName == "预计请购")
            {
                x4 = "查看请购单";

            }
            else
            {
                x4 = "其他";
                MessageBox.Show("本列无明细数据", "软件提示");
            }

            if (x4 != "其他")
            {
                MXCX xx = new MXCX(x1, x2, x3, x4, x5);
                CHAXUN f = new CHAXUN();
                f.cdgzzx += new CHAXUN.Cdgzzx(textBox2_TextChanged);
                f.Show(this);
            }
        }
    }
     
}
